﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class NhaCungCapDAO
    {
        public int ThemNCC(NhaCungCapDTO dto)
        {
            SqlConnection conn = DataProvider.ConnectionDB();
            string sql = "insert into NHACUNGCAP values(";
            sql += "N'"+dto.MANCC+"',";
            sql += "N'" + dto.TENNCC + "',";
            sql += "N'" + dto.DIACHI + "',";
            sql += "N'" + dto.SODIENTHOAI + "',";
            sql += "N'" + dto.GHICHU + "')";

            SqlCommand command = new SqlCommand(sql, conn);
            int kq = command.ExecuteNonQuery();
            conn.Close();
            if (kq <= 0)
                return 0;
            return 1;
        }

        public List<NhaCungCapDTO> HienThi()
        {
            string sql = "select * from NHACUNGCAP";
            SqlConnection conn = DataProvider.ConnectionDB();
            SqlCommand command = new SqlCommand(sql, conn);
            SqlDataReader reader = command.ExecuteReader();
            
            List<NhaCungCapDTO> danhsach = new List<NhaCungCapDTO>();
            while (reader.Read())
            {
                NhaCungCapDTO dto = new NhaCungCapDTO();
                dto.MANCC = reader.GetString(0);
                dto.TENNCC = reader.GetString(1);
                dto.DIACHI = reader.GetString(2);
                dto.SODIENTHOAI = reader.GetString(3);
                dto.GHICHU = reader.GetString(4);
                danhsach.Add(dto);
            }
            conn.Close();
            return danhsach;
        }

        public int CapNhat(NhaCungCapDTO dto)
        {
            SqlConnection conn = DataProvider.ConnectionDB();
            string sql = "update NHACUNGCAP ";
            sql += "set MANCC=";
            sql += "N'" + dto.MANCC + "',";
            sql += "TENNCC=";
            sql += "N'" + dto.TENNCC + "',";
            sql += "DIACHI=";
            sql += "N'" + dto.DIACHI + "',";
            sql += "SODIENTHOAI=";
            sql += "N'" + dto.SODIENTHOAI + "',";
            sql += "GHICHU=";
            sql += "N'" + dto.GHICHU + "' ";
            sql += "where MANCC=";
            sql += "'" + dto.MANCC + "'";

            SqlCommand command = new SqlCommand(sql, conn);
            int kq = command.ExecuteNonQuery();
            conn.Close();
            if (kq <= 0)
                return 0;
            return 1;
        }

        public int Xoa(NhaCungCapDTO dto)
        {
            SqlConnection conn = DataProvider.ConnectionDB();
            string sql = "Delete NHACUNGCAP ";
            sql += "where MANCC=";
            sql += "'" + dto.MANCC + "'";
            SqlCommand command = new SqlCommand(sql, conn);
            int kq = command.ExecuteNonQuery();
            conn.Close();
            if (kq <= 0)
                return 0;
            return 1;
        }

        public int KiemTraMANCC(NhaCungCapDTO dto)
        {
            SqlConnection conn = DataProvider.ConnectionDB();
            string sql = "Select count(*) from NHACUNGCAP where MANCC=";
            sql += "'" + dto.MANCC + "'";
            SqlCommand command = new SqlCommand(sql, conn);
            int kq = (int)command.ExecuteScalar();
            conn.Close();
            if (kq <= 0)
                return 0;
            return 1;
        }

        
    }
}
